﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using System.Runtime.InteropServices;

namespace Profari_Heldur_Utan_Um_Prof
{
    public partial class CreateTask : Form
    {
        #region SQL connection
        private static SqlConnection con = new SqlConnection("Data Source=orninn.com;Initial Catalog=Notendaprofanir;Persist Security Info=True;User ID=SonjaNew;Password=Léttjógúrt ");
        private static DataTable dt = new DataTable();
        private static DataSet ds = new DataSet();
        SqlDataReader r = null;
        #endregion

        #region Static variables
        public static int globalGroupID = 0;
        public static int radCreateGroupNameValue = 0;
        public static int radSelectGroupNameValue = 0;
        public static string cmbSelectGroupNameValue;
        public static string txtCreateGroupNameValue;
        public static string txtEvaluationNameValue;
        public static string txtWebPageValue;
        public static string txtTaskNameValue;
        public static string riTaskDescriptionValue;
        public static string riTaskScenarioValue;
        public static int chkDescriptionTaskValue;
        public static List<Tasks> taskList = new List<Tasks>();
        public static ArrayList TaskID = new ArrayList();
        #endregion

        #region Variables
        public int createTaskPressed = 0;
        public int buttonGroupNamePressed = 0;
        public int buttonTaskNamePressed = 0;
        public int buttonTaskDescriptionPressed = 0;
        public static int nextPressed = 0;
        #endregion

        public struct Tasks
        {
            public string createNewGroup;
            public string nameOfEvaluation;
            public string userWebPage;
            public string task;
            public string taskDescript;
            public string scenarioTask;
        }

        public CreateTask()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void CreateTask1_Load(object sender, EventArgs e)
        {
            setCreateTaskVariables();
            getGroupName();
            fillInfo();
            
            #region Shows boxes when form is loaded
            cmbGroupNames.Show();
            txtNewGroupName.Show();
            #endregion

            #region Hides boxes when form is loaded
            btnUpdate.Hide();
            lblGroupInfo.Hide();
            lblTaskName.Hide();
            lblWarningText.Hide();
            lblStar6.Hide();
            lblDescriptionTask.Hide();
            btnCreateTask.Hide();
            #endregion

            #region If/Else statements
            if (radCreateGroupNameValue == 1)
            {
                cmbGroupNames.Hide();
            }
            else if (radSelectGroupNameValue == 1)
            {
                txtNewGroupName.Hide();
            }
            if (cmbGroupNames.Items.Count == 0)
                radSelectGroupName.Hide();

            if (chkDescriptionTask.Checked == false)
            {
                riTaskScenario.Enabled = false;
            }
            if (radSelectGroupNameValue == 1)
            {
                radSelectGroupName.Checked = true;
            }
            else
            {
                radCreateGroupName.Checked = true;
            }
            if (nextPressed > 0)
            {
                radCreateGroupName.Enabled = false;
                radSelectGroupName.Enabled = false;
                txtNewGroupName.Enabled = false;
                cmbGroupNames.Enabled = false;
                txtEvaluationName.Enabled = false;
                txtWebPage.Enabled = false;
            }
            if (radSelectGroupNameValue == 1)
            {
                cmbGroupNames.SelectedItem = cmbSelectGroupNameValue;
            }
            #endregion

        }

        #region Functions
        //Fill the listbox with values that have been typed in the form
        private void fillInfo()
        {
            foreach (Tasks temp in taskList)
            {
                listBox1.Items.Add(temp.task);
            }
        }        
        
        //Data from textboxes are put in the static variables
        public void getStaticVariables()
        {
            if(radCreateGroupName.Checked == true)
            {
                radCreateGroupNameValue = 1;
                radSelectGroupNameValue = 0;            
                txtCreateGroupNameValue = txtNewGroupName.Text;
            }
            if (radSelectGroupName.Checked == true)
            {
                radCreateGroupNameValue = 0;
                radSelectGroupNameValue = 1;
                cmbSelectGroupNameValue = cmbGroupNames.SelectedItem.ToString();
            }
 
            txtEvaluationNameValue = txtEvaluationName.Text;
            txtWebPageValue = txtWebPage.Text;
            txtTaskNameValue = txtTaskName.Text;
            riTaskDescriptionValue = riDescriptionOfTask.Text;
            riTaskScenarioValue = riTaskScenario.Text;
            chkDescriptionTaskValue = chkDescriptionTask.CheckState.GetHashCode();
            foreach (Tasks temp in taskList)
            {
                if (temp.task == (string)listBox1.SelectedItem)
                {

                }
            }
        }

        //The variables in this forms get the value from the static variables
        public void setCreateTaskVariables()
        {
            if (radCreateGroupNameValue == 1)
            {
                radCreateGroupName.Checked = true;
                txtNewGroupName.Text = txtCreateGroupNameValue;
                txtNewGroupName.Show();
                cmbGroupNames.Hide();
            }
            else if (radSelectGroupNameValue == 1)
            {
                radSelectGroupName.Checked = true;
                cmbGroupNames.SelectedItem = cmbSelectGroupNameValue;
                cmbGroupNames.Text = cmbSelectGroupNameValue;
                txtNewGroupName.Hide();
                cmbGroupNames.Show();
            }
            
            
            txtEvaluationName.Text = txtEvaluationNameValue;
            txtWebPage.Text = txtWebPageValue;
            txtTaskName.Text = txtTaskNameValue;
            riDescriptionOfTask.Text = riTaskDescriptionValue;
            riTaskScenario.Text = riTaskScenarioValue;
            if (chkDescriptionTaskValue.Equals(1))
            {
                chkDescriptionTask.CheckState = CheckState.Checked;
            }

        }
/* Má hugsanlega henda út, sjá í lokin
        public void setVariablesToNull()
        {
            radCreateGroupNameValue = 0;
            radSelectGroupNameValue = 0;
            cmbSelectGroupNameValue = "";
            txtCreateGroupNameValue = "";
            txtEvaluationNameValue = "";
            txtWebPageValue = "";
            txtTaskNameValue = "";
            riTaskDescriptionValue = "";
            riTaskScenarioValue = "";
            chkDescriptionTaskValue = 0;
            CheckBoxes.chkIntroSessionValue = 0;
            CheckBoxes.chkInfoUserBeforeValue = 0;
            CheckBoxes.chkNameValue = 0;
            CheckBoxes.chkSsnValue = 0;
            CheckBoxes.chkMobileValue = 0;
            CheckBoxes.chkAddressValue = 0;
            CheckBoxes.chkGenderValue = 0;
            CheckBoxes.chkBirthYearValue = 0;
            CheckBoxes.chkOtherValue = 0;
            CheckBoxes.chkInfoUserAfterValue = 0;
            CheckBoxes.chkRadQuestions = 0;
            CheckBoxes.radEssayValue = 0;
            CheckBoxes.radSurveyValue = 0;
            CheckBoxes.txtEmailValue = "";
            CheckBoxes.chkEmailValue = 0;
            taskList.Clear();
        }
 * */
        #endregion

        #region Events
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //txtWebPage.Text = webBrowser1.Document.Url.ToString();
            toolStripUrlText.Text = webBrowser1.Document.Url.ToString();
        }

        private void radCreateGroupName_CheckedChanged(object sender, EventArgs e)
        {
            if (radCreateGroupName.Checked == true)
            {
                txtNewGroupName.Show();
                cmbGroupNames.Hide();
            }
        }

        private void radSelectGroupName_CheckedChanged(object sender, EventArgs e)
        {
            if (radSelectGroupName.Checked == true)
            {
                cmbGroupNames.Show();
                txtNewGroupName.Hide();
            }
        }

        private void chkDescriptionTask_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDescriptionTask.CheckState == CheckState.Checked)
            {
                riTaskScenario.Enabled = true;
                lblStar6.Show();
            }
            else
            {
                riTaskScenario.Enabled = false;
                lblStar6.Hide();
            }
        }

        private void cmbGroupNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbSelectGroupNameValue = (string)cmbGroupNames.SelectedItem;
        }        
        
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (Tasks temp in taskList)
            {
                if (temp.task == (string)listBox1.SelectedItem)
                {
                    txtTaskName.Text = temp.task;
                    riDescriptionOfTask.Text = temp.taskDescript;
                    if (temp.scenarioTask != "")
                    {
                        riTaskScenario.Text = temp.scenarioTask;
                        chkDescriptionTask.Checked = true;
                    }
                    else
                    {
                        riTaskScenario.Text = "";
                        chkDescriptionTask.Checked = false;
                    }
                }
            }
            btnCreate.Hide();
            btnUpdate.Hide();
            btnCreateTask.Show();

            radCreateGroupName.Enabled = false;
            radSelectGroupName.Enabled = false;
            cmbGroupNames.Enabled = false;
            txtNewGroupName.Enabled = false;
            txtWebPage.Enabled = false;
            txtEvaluationName.Enabled = false;
            txtTaskName.Enabled = false;
            riDescriptionOfTask.Enabled = false;
            chkDescriptionTask.Enabled = false;
            riTaskScenario.Enabled = false;
        }
        #endregion

        #region Buttons
        private void toolStripBtnBack_Click(object sender, EventArgs e)
        {
            if (webBrowser1.CanGoBack)
            {
                webBrowser1.GoBack();
            }
        }

        private void toolStripBtnForward_Click(object sender, EventArgs e)
        {
            if (webBrowser1.CanGoForward)
            {
                webBrowser1.GoForward();
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
                webBrowser1.Navigate(txtWebPage.Text);
                toolStripUrlText.Text = txtWebPage.Text;
        }

        private void btnCreateTask_Click(object sender, EventArgs e)
        {
            txtTaskName.Clear();
            riDescriptionOfTask.Clear();
            chkDescriptionTask.Checked = false;
            riTaskScenario.Clear();
            btnCreate.Show();
            btnUpdate.Hide();
            btnCreateTask.Hide();


            radCreateGroupName.Enabled = false;
            radSelectGroupName.Enabled = false;
            cmbGroupNames.Enabled = false;
            txtNewGroupName.Enabled = false;
            txtWebPage.Enabled = false;
            txtEvaluationName.Enabled = false;
            txtTaskName.Enabled = true;
            riDescriptionOfTask.Enabled = true;
            chkDescriptionTask.Enabled = true;
            riTaskScenario.Enabled = true;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {

            if ((radCreateGroupName.Checked == true && txtNewGroupName.Text == "") ||
    (radSelectGroupName.Checked == true && cmbGroupNames.SelectedIndex == -1) ||
    txtEvaluationName.Text == "" || txtWebPage.Text == "" ||
    txtTaskName.Text == "" || riDescriptionOfTask.Text == "" ||
    (chkDescriptionTask.Checked == true && riTaskScenario.Text == ""))
            {

                MessageBox.Show("Please fill in the boxes marked with red star");
                lblWarningText.Show();
                lblWarningText.Text = "Please type some text that are marked with red star";
            }
            else
            {
                createTaskPressed++;
                listBox1.Items.Add(txtTaskName.Text);
                if (chkDescriptionTask.CheckState == CheckState.Checked)
                {
                    taskList.Add(new Tasks() { task = txtTaskName.Text, taskDescript = riDescriptionOfTask.Text, scenarioTask = riTaskScenario.Text });
                }
                else if(chkDescriptionTask.CheckState == CheckState.Unchecked)
                {
                    riTaskScenario.Text = "";
                    taskList.Add(new Tasks() { task = txtTaskName.Text, taskDescript = riDescriptionOfTask.Text, scenarioTask = riTaskScenario.Text });
                }
                txtTaskName.Clear();
                riDescriptionOfTask.Clear();
                chkDescriptionTask.Checked = false;
                riTaskScenario.Clear();
                radCreateGroupName.Enabled = false;
                radSelectGroupName.Enabled = false;
                cmbGroupNames.Enabled = false;
                txtNewGroupName.Enabled = false;
                txtWebPage.Enabled = false;
                txtEvaluationName.Enabled = false;
                btnCreateTask.Hide();
            }


        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int selected = listBox1.SelectedIndex;
            listBox1.Items.RemoveAt(selected);
            listBox1.Items.Insert(selected, txtTaskName.Text);
            taskList.RemoveAt(selected);
            var task =  new Tasks() { task = txtTaskName.Text, taskDescript = riDescriptionOfTask.Text, scenarioTask = riTaskScenario.Text };
            if (chkDescriptionTask.CheckState == CheckState.Unchecked)
            {
                chkDescriptionTask.Checked = false;
                riTaskScenario.Text = "";
            }
            else
            {
                taskList.Insert(selected, task);
            }
            txtTaskName.Clear();
            riDescriptionOfTask.Clear();
            chkDescriptionTask.Checked = false;
            riTaskScenario.Clear();
            btnUpdate.Hide();
            btnCreate.Show();
        }

        private void btnEditTask_Click(object sender, EventArgs e)
        {
            if (createTaskPressed > 0)
            {
                btnUpdate.Show();
                btnCreate.Hide();
            }
            
            if (listBox1.Items.Count == 0)
            {
                MessageBox.Show("There are no items to edit, please create task first");
            }
            else
            {

                foreach (Tasks temp in taskList)
                {
                    if (temp.task == (string)listBox1.SelectedItem)
                    {
                        txtTaskName.Text = temp.task;
                        riDescriptionOfTask.Text = temp.taskDescript;
                        if (temp.scenarioTask != "")
                        {
                            riTaskScenario.Text = temp.scenarioTask;
                            chkDescriptionTask.Checked = true;
                        }
                    }

                    radCreateGroupName.Enabled = false;
                    radSelectGroupName.Enabled = false;
                    cmbGroupNames.Enabled = false;
                    txtNewGroupName.Enabled = false;
                    txtWebPage.Enabled = false;
                    txtEvaluationName.Enabled = false;
                    txtTaskName.Enabled = true;
                    riDescriptionOfTask.Enabled = true;
                    chkDescriptionTask.Enabled = true;
                    riTaskScenario.Enabled = true;
                }
                
            }
        }

        private void btnDeleteTask_Click(object sender, EventArgs e)
        {
            DialogResult deleteTasks = MessageBox.Show("Are you sure you want to delete this Task?", "Quit", MessageBoxButtons.YesNo);
            if (deleteTasks == DialogResult.Yes)
            {
                taskList.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                btnCreate.Show();
                btnEditTask.Hide();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult cancelProject = MessageBox.Show("Are you sure you want to stop making new project?", "Quit", MessageBoxButtons.YesNo);
            if (cancelProject == DialogResult.Yes)
            {                
                //setVariablesToNull();
                System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenTesterInterface));
                testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
                testerInterface.Start();
                this.Close();                
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {   
            if(radSelectGroupName.Checked == true)
                getGroupID();
            if ((radCreateGroupName.Checked == true && txtNewGroupName.Text == "") || 
                (radSelectGroupName.Checked == true && cmbGroupNames.SelectedIndex == -1) || 
                txtEvaluationName.Text == "" || txtWebPage.Text == "" || listBox1.Items.Count == 0)
            {
               
                MessageBox.Show("Please fill in the boxes marked with red star");
                lblWarningText.Show();
                lblWarningText.Text = "Please type some text that are marked with red star";
            }
            else
            {
                getStaticVariables();
                nextPressed++;
                System.Threading.Thread checkBoxes = new System.Threading.Thread(new System.Threading.ThreadStart(OpenCheckBoxes));
                checkBoxes.SetApartmentState(System.Threading.ApartmentState.STA);
                checkBoxes.Start();
                this.Close();
            }
        }

        private void btnGroupInfo_Click(object sender, EventArgs e)
        {
            if (buttonGroupNamePressed % 2 == 1)
            {
                lblGroupInfo.Hide();
                buttonGroupNamePressed++;
            }
            else if (buttonGroupNamePressed % 2 == 0)
            {
                lblGroupInfo.Show();
                buttonGroupNamePressed++;
            }
        }

        private void btnTaskName_Click(object sender, EventArgs e)
        {
            if (buttonTaskNamePressed % 2 == 1)
            {
                lblTaskName.Hide();
                buttonTaskNamePressed++;
            }
            else if(buttonTaskNamePressed % 2 == 0)
            {
                lblTaskName.Show();
                buttonTaskNamePressed++;
            }
        }

        private void btnDescriptionTask_Click(object sender, EventArgs e)
        {
            if (buttonTaskDescriptionPressed % 2 == 1)
            {
                lblDescriptionTask.Hide();
                buttonTaskDescriptionPressed++;
            }
            else if (buttonTaskDescriptionPressed % 2 == 0)
            {
                lblDescriptionTask.Show();
                buttonTaskDescriptionPressed++;
            }
        }
        #endregion

        #region Sql Commands
        private void getGroupName()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT name FROM Groups", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    cmbGroupNames.Items.Add(r["Name"]);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
            Hashtable hash = new Hashtable();
            int cnt = 0;
            foreach (string item in cmbGroupNames.Items)
            {
                if (!hash.ContainsValue(item))
                {
                    hash[cnt++] = item;
                }
            }

            cmbGroupNames.Items.Clear();
            for (int i = 0; i < cnt; i++)
            {
                cmbGroupNames.Items.Add(hash[i]);
            }
        }

        private void getGroupID()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT id FROM Groups WHERE name='" + (string)cmbGroupNames.SelectedItem + "'", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    globalGroupID = r.GetInt32(0);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }
        #endregion

        #region OpenForms
        public void OpenTesterInterface()
        {
            Application.Run(new TesterInterface());
        }

        public void OpenCheckBoxes()
        {
            Application.Run(new CheckBoxes());
        }
        #endregion

    }
}
